package com.ben.multiple.util;

/**
 * 日期、时间工具
 *
 * @author BenJin Yin
 * @date 2019/10/25
 */
public final class DateTimeUtils {
    private DateTimeUtils() {
    }

    /**
     * 日期、时间、特殊字符和空白字符  正则表达式
     */
    private static final String PURE_LOCAL_DATE_TIME_REGEX = "\\d{14}";
    private static final String PURE_LOCAL_DATE_REGEX = "\\d{8}";
    private static final String PURE_LOCAL_TIME_REGEX = "\\d{6}";
    private static final String PURE_YEAR_MONTH_REGEX = "\\d{6}";
    private static final String SPECIAL_AND_BLANK_CHAR_REGEX = "(年)?(月)?(日)?(：)?(:)?(/)?(-)?(\\s)?(\\r)?(\\n)?";

    /**
     * 校验完整时间
     *
     * @param localDateTime 年月日 时分秒
     * @return VerifyResult
     */
    public static VerifyResult verifyLocalDateTimeFormat(String localDateTime) {
        localDateTime = removeBlankAndSpecialChar(localDateTime);
        if (localDateTime == null || !localDateTime.matches(PURE_LOCAL_DATE_TIME_REGEX)) {
            return VerifyResult.fail();
        }
        return VerifyResult.ok(localDateTime);
    }

    /**
     * 校验日期
     *
     * @param localDate 年月日
     * @return VerifyResult
     */
    public static VerifyResult verifyLocalDateFormat(String localDate) {
        localDate = removeBlankAndSpecialChar(localDate);
        if (localDate == null || !localDate.matches(PURE_LOCAL_DATE_REGEX)) {
            return VerifyResult.fail();
        }
        return VerifyResult.ok(localDate);
    }

    /**
     * 校验时间
     *
     * @param localTime 时分秒
     * @return VerifyResult
     */
    public static VerifyResult verifyLocalTimeFormat(String localTime) {
        localTime = removeBlankAndSpecialChar(localTime);
        if (localTime == null || !localTime.matches(PURE_LOCAL_TIME_REGEX)) {
            return VerifyResult.fail();
        }
        return VerifyResult.ok(localTime);
    }

    /**
     * 校验年月
     *
     * @param yearMonth 年-月
     * @return VerifyResult
     */
    public static VerifyResult verifyYearMonthFormat(String yearMonth) {
        yearMonth = removeBlankAndSpecialChar(yearMonth);
        if (yearMonth == null || !yearMonth.matches(PURE_YEAR_MONTH_REGEX)) {
            return VerifyResult.fail();
        }
        return VerifyResult.ok(yearMonth);
    }

    /**
     * 移除特殊字符 和 空白字符
     *
     * @param str String
     * @return String
     */
    private static String removeBlankAndSpecialChar(String str) {
        return str == null ? null : str.trim().replaceAll(SPECIAL_AND_BLANK_CHAR_REGEX, "");
    }

    /**
     * 日期、时间校验结果；flag==true：格式正确，返回处理后的 时间；flag==false：格式错误，不返回 时间
     * 对外只提供属性的 get 方法，确保属性值不变
     */
    public static final class VerifyResult {
        private Boolean flag;
        private String result;

        private VerifyResult(Boolean flag, String result) {
            this.flag = flag;
            this.result = result;
        }

        private static VerifyResult ok(String result) {
            return new VerifyResult(true, result);
        }

        private static VerifyResult fail() {
            return new VerifyResult(false, null);
        }

        public Boolean getFlag() {
            return flag;
        }

        public String getResult() {
            return result;
        }
    }
}
